var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), 
i = require("../../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    serviceList:[],
    serviceChildList:[],
    goodsList:[],
    pageIndex:1,
    pageSize:10,
    longitude:0,
    cateId:0,
    latitude:0,
    isHasChild:0,
    keyword:'',
    activeKey: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onHttpInit()
  },
  onShow:function(){
    // this.data.pageIndex = 1;
    // this.onHttpInit()
    const param = getApp().globalData.tabBarParam;
    if(param){
        this.handleJump(param.type);
        setTimeout(() => {
            this.onChangeType(param.type);
        }, 2000); // 3000 毫秒 = 3 秒
    }
    
  },
  onChangeType(typeIndex) {
    this.setData({ activeKey:typeIndex});
  },
  handleJump: function (typeIndex) {
    
    getApp().globalData.tabBarParam = null;
    setTimeout(() => {
        const param = this.data.serviceList[typeIndex];
        console.log('接收参数:', this.data.serviceList);
        var type_id = param.id;
        this.data.cateId = type_id;
        var childCount = param.childCount;
        if(childCount > 0){
            this.data.pageIndex = 1;
            this.onHttpTypeChildList(res);
            this.setData({
                serviceChildList:[],
                goodsList:[],
                isHasChild:1
            })
        }else{
            this.data.pageIndex = 1;
            this.setData({
                serviceChildList:[],
                goodsList:[],
                isHasChild:0
            })
            this.onClickServiceType(type_id);
        }
    }, 2000);
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost("/product/type/list", {
      parent_id: 0,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      longitude: that.data.longitude,
      latitude: that.data.latitude,
    }, function (res) {
      console.log("获取商家服务类型返回数据:", res)
      if(res.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1;
        that.data.cateId = res.data.data.types[0].id;
        that.setData({
            serviceList: res.data.data.types,
            goodsList: res.data.data.products,
          });
      }
    }, function (a) {
      console.log("获取商家服务类型返回数据 err:", a);
    });
  },
  onHttpTypeChildList: function (res) {
    var type_id = res.target.dataset.id
    var that = this;
    fetchApi.httpPost("/product/type/childList", {
      type_id:type_id,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
    }, function (a) {
      console.log("获取商家服务类型返回数据:", a)
      that.setData({
        serviceChildList: a.data.data.types,
      });
    }, function (a) {
      console.log("获取商家服务类型返回数据 err:", a);
    });
  },
  onChange(e){
    console.log('onChange',e)
    this.data.keyword = e.detail.value
    this.setData({
        keyword: e.detail.value
    });
  },
  onSearch(val){
    console.log('onSearch',val)
    // this.data.keyword = val.detail;
    this.data.pageIndex = 1
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      serviceChildList:[],
      goodsList:[],
      
    })
    this.onClickServiceType(this.data.cateId);
  },
  handleType(res){
    var type_id = res.target.dataset.id;
    this.data.cateId = type_id;
    var childCount = res.target.dataset.childCount;
    if(childCount > 0){
      this.data.pageIndex = 1;
      this.onHttpTypeChildList(res);
      this.setData({
        serviceChildList:[],
        goodsList:[],
        isHasChild:1
      })
    }else{
        this.data.pageIndex = 1;
        this.setData({
          serviceChildList:[],
          goodsList:[],
          isHasChild:0
        })
      this.onClickServiceType(type_id);
    }
  },
  onClickServiceType: function (type_id) {
    var that = this;
    fetchApi.httpPost("/product/list", {
      type_id:type_id,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      longitude: that.data.longitude,
      latitude: that.data.latitude,
      keyword: that.data.keyword,
    }, function (a) {
      console.log("获取商家服务类型返回数据:", a)
      if(a.data.totalcount > 0){
        that.data.pageIndex = that.data.pageIndex + 1;
        var products = a.data.data;
        if(products.length > 0){
          for (var index in products) {
            that.data.goodsList.splice(that.data.goodsList.length, 0, products[index]);
          }
        }
        that.setData({
          goodsList: that.data.goodsList,
        });
      }
      
        that.setData({
            loading: false,
            netWork: true,
            isLoading: false,
        });
    }, function (a) {
      console.log("获取商家服务类型返回数据 err:", a);
    });
  },
  goProductList(e){
    var parentId = e.currentTarget.dataset.parentId;
    var id = e.currentTarget.dataset.id;
    if(parentId == 0){
      wx.navigateTo({
        url: '/pages/product/list/index?type_id=' +id ,
      })
    }else{
      wx.navigateTo({
        url: '/pages/product/list/index?type_id=' +parentId + '&type_child_id=' +  id,
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  searchScrollLower(){
    this.onClickServiceType(this.data.cateId)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onClickServiceType(this.data.cateId)
  },
})